package com.hskn.hss.module.empPlan.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.constant.Constants;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.file.FileTypeUtils;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.carPlan.vo.WorkGroupByCarPlanVo;
import com.hskn.hss.module.common.ienum.ExcelTypeEnum;
import com.hskn.hss.module.common.to.ExcelReaderTO;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.empPlan.bo.WorkKaoQinAndWorkGroup;
import com.hskn.hss.module.empPlan.entity.EmpPlan;
import com.hskn.hss.module.empPlan.mapper.EmpPlanMapper;
import com.hskn.hss.module.empPlan.service.EmpPlanService;
import com.hskn.hss.module.empPlan.to.*;
import com.hskn.hss.module.empPlan.vo.*;
import com.hskn.hss.module.empPlanDate.entity.EmpPlanDate;
import com.hskn.hss.module.empPlanDate.mapper.EmpPlanDateMapper;
import com.hskn.hss.module.empPlanDate.service.EmpPlanDateService;
import com.hskn.hss.module.empPlanEmployee.entity.EmpPlanEmployee;
import com.hskn.hss.module.empPlanEmployee.mapper.EmpPlanEmployeeMapper;
import com.hskn.hss.module.empPlanEmployee.service.EmpPlanEmployeeService;
import com.hskn.hss.module.empPlanWorkGroup.entity.EmpPlanWorkGroup;
import com.hskn.hss.module.empPlanWorkGroup.mapper.EmpPlanWorkGroupMapper;
import com.hskn.hss.module.empWorkPoints.entity.EmpWorkPoints;
import com.hskn.hss.module.empWorkPoints.service.IEmpWorkPointsService;
import com.hskn.hss.module.employeePunchCard.entity.EmployeePunchCard;
import com.hskn.hss.module.employeePunchCard.mapper.EmployeePunchCardMapper;
import com.hskn.hss.module.employeeTrack.service.EmployeeTrackService;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.gridSnapshot.entity.GridSnapshot;
import com.hskn.hss.module.gridSnapshot.mapper.GridSnapshotMapper;
import com.hskn.hss.module.gridbind.entity.GridBind;
import com.hskn.hss.module.gridbind.mapper.GridBindMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.sysDictType.service.SysDictTypeService;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlopetation.service.ITlOperationService;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.module.workgroup.mapper.WorkGroupMapper;
import com.hskn.hss.module.workgroup.service.IWorkGroupService;
import com.hskn.hss.module.workkaoqin.entity.WorkKaoqin;
import com.hskn.hss.module.workkaoqin.mapper.WorkKaoqinMapper;
import com.hskn.hss.module.workkaoqin.service.IWorkKaoqinService;
import com.hskn.hss.module.workrule.entity.WorkRule;
import com.hskn.hss.module.workrule.mapper.WorkRuleMapper;
import com.hskn.hss.utils.*;
import com.hskn.hss.utils.page.PageVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class EmpPlanServiceImpl extends ServiceImpl<EmpPlanMapper, EmpPlan> implements EmpPlanService {

    @Resource
    private EmpPlanMapper empPlanMapper;
    @Resource
    private EmpPlanEmployeeMapper empPlanEmployeeMapper;
    @Resource
    private EmpPlanDateMapper empPlanDateMapper;
    @Resource
    private GridMapper gridMapper;
    @Resource
    private GridSnapshotMapper gridSnapshotMapper;
    @Resource
    private WorkRuleMapper workRuleMapper;
    @Resource
    private TlEmployeeMapper tlEmployeeMapper;
    @Resource
    private WorkKaoqinMapper workKaoqinMapper;
    @Autowired
    private IWorkKaoqinService workKaoqinService;
    @Resource
    private WorkGroupMapper workGroupMapper;
    @Resource
    private SysDepartMapper sysDepartMapper;
    @Resource
    private EmpPlanWorkGroupMapper empPlanWorkGroupMapper;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;
    @Autowired
    private ITlEmployeeService tlEmployeeService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IGridService gridService;
    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;
    @Resource
    private TlWorkTypeMapper tlWorkTypeMapper;
    @Autowired
    private ITlOperationService operationService;
    @Autowired
    private IWorkGroupService workGroupService;
    @Autowired
    private EmpPlanDateService empPlanDateService;
    @Autowired
    private EmpPlanEmployeeService empPlanEmployeeService;
    @Resource
    private EmployeePunchCardMapper employeePunchCardMapper;
    @Resource
    private GridBindMapper gridBindMapper;
    @Autowired
    private EmployeeTrackService employeeTrackService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITlEmployeeService employeeService;
    @Autowired
    private SysDictTypeService dictTypeService;
    @Autowired
    private IEmpWorkPointsService empWorkPointsService;

    public static long getDateUpgrade(Calendar c) {
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
        return c.getTimeInMillis();
    }

    public static Boolean checkDate(List<EmpPlanDateTo> empPlanDateToList, int index) {
        int end = 0;
        if (empPlanDateToList.size() == index) {//结束条件，遍历完数组
            return null;
        } else {
            for (int i = index; i < empPlanDateToList.size(); i++) {
                if (i < empPlanDateToList.size() - 1) {
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(DateUtil.parse(empPlanDateToList.get(i).getDate(), "yyyy-MM-dd"));
                    calendar.add(Calendar.DATE, 1); //把日期往后增加一天,整数  往后推,负数往前移动
                    Date date1 = calendar.getTime();
                    Date date2 = DateUtil.parse(String.valueOf(empPlanDateToList.get(i + 1).getDate()), "yyyy-MM-dd");
                    if (date1.getTime() == date2.getTime()) {
                        end = i;
                    } else {
                        if (i > index)
                            end = end + 1;
                        break;
                    }
                } else {
                    if (end == empPlanDateToList.size() - 2) {
                        end = empPlanDateToList.size() - 1;
                        break;
                    }
                }
            }
            //相等说明不连续
            //连续
            return index != end;
        }
    }

    @Override
    @DataScope(deptAlias = "depid", userAlias = "manager_id, bind_person_ids, create_uid, update_uid")
    public EmpPlanPageVo masterList(BaseEntity entity, EmpPlanTo empPlanTo) {
        //网格列表
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            queryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<Grid> gridList = gridMapper.selectList(queryWrapper);
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(empPlanTo.getStartDate()) && StringUtils.isNotEmpty(empPlanTo.getEndDate())) {
            empPlanDateQueryWrapper.lambda().le(EmpPlanDate::getStartDate, empPlanTo.getEndDate());
            empPlanDateQueryWrapper.lambda().ge(EmpPlanDate::getEndDate, empPlanTo.getStartDate());
        }
        List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
        Map<String, List<EmpPlanDate>> empPlanDateMap = empPlanDateList.stream().collect(Collectors.groupingBy(EmpPlanDate::getPlanId));

        QueryWrapper<EmpPlan> empPlanQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(empPlanTo.getPlanName())) {
            empPlanQueryWrapper.lambda().like(EmpPlan::getPlanName, empPlanTo.getPlanName());
        }
        if (StringUtils.isNotEmpty(empPlanTo.getDepartmentId())) {
            List<String> departmentList = departService.getChildIdsByIId(empPlanTo.getDepartmentId());
            empPlanQueryWrapper.lambda().in(EmpPlan::getDepartmentId, departmentList);
        }
        //按创建时间排序
        empPlanQueryWrapper.lambda().orderByDesc(EmpPlan::getCreateTime);
        List<EmpPlan> empPlanList = empPlanMapper.selectList(empPlanQueryWrapper);
        List<EmpPlanMasterVo> empPlanMasterVoList = new ArrayList<>();
        for (EmpPlan empPlan : empPlanList) {

            List<EmpPlanDate> empPlanDateList1 = empPlanDateMap.get(empPlan.getId());
            if (null != empPlanDateList1) {
                EmpPlanMasterVo empPlanMasterVo = new EmpPlanMasterVo();
                empPlanMasterVo.setPlanId(empPlan.getId());
                empPlanMasterVo.setPlanName(empPlan.getPlanName());
                Grid grid = gridMap.get(empPlan.getGridId());
                if (null != grid) {
                    empPlanMasterVo.setGridId(grid.getGridId());
                    empPlanMasterVo.setGridName(grid.getName());
                }
                WorkRule workRule = workRuleMap.get(empPlan.getWorkRuleId());
                if (null != workRule) {
                    empPlanMasterVo.setWorkRuleId(workRule.getWorkRuleId());
                    empPlanMasterVo.setWorkRuleName(workRule.getRuleName());
                }
                SysDepart sysDepart = sysDepartMap.get(empPlan.getDepartmentId());
                if (null != sysDepart) {
                    empPlanMasterVo.setDepartmentId(sysDepart.getId());
                    empPlanMasterVo.setDepartmentName(sysDepart.getDepartName());
                }

                empPlanMasterVoList.add(empPlanMasterVo);
            }
        }
        return empPlanPage(empPlanTo, empPlanMasterVoList);
    }

    @Override
    @DataScope(deptAlias = "depid", userAlias = "manager_id, bind_person_ids, create_uid, update_uid")
    public EmpPlanPageVo detailList(BaseEntity entity, EmpPlanTo empPlanTo) throws ParseException {
        //网格列表
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            queryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<Grid> gridList = gridMapper.selectList(queryWrapper);
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());
        Map<String, TlEmployee> tlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));


        List<WorkKaoqin> workKaoQinList = workKaoqinMapper.selectList(new QueryWrapper<>());
        Map<String, WorkKaoqin> workKaoQinMap = workKaoQinList.stream().collect(Collectors.groupingBy(WorkKaoqin::getWorkKaoqinId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<SysDepart> sysDepartList = sysDepartMapper.selectList(new QueryWrapper<>());
        Map<String, SysDepart> sysDepartMap = sysDepartList.stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));


        QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
        if (null != empPlanTo.getEmployeeList() && !empPlanTo.getEmployeeList().isEmpty()) {
            empPlanEmployeeQueryWrapper.lambda().in(EmpPlanEmployee::getEmployeeId, empPlanTo.getEmployeeList());
        }
        empPlanEmployeeQueryWrapper.lambda().eq(EmpPlanEmployee::getPlanId, empPlanTo.getPlanId());
        List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);


        QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
        empPlanDateQueryWrapper.lambda().eq(EmpPlanDate::getPlanId, empPlanTo.getPlanId());
        List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
        Map<String, List<EmpPlanDate>> empPlanDateMap = empPlanDateList.stream().collect(Collectors.groupingBy(EmpPlanDate::getPlanId));

        List<EmpPlanWorkGroup> empPlanWorkGroupList = empPlanWorkGroupMapper.selectList(new QueryWrapper<>());
        Map<String, List<EmpPlanWorkGroup>> empPlanWorkGroupMap = empPlanWorkGroupList.stream().collect(Collectors.groupingBy(EmpPlanWorkGroup::getEmployeeId));

        List<EmpPlanMasterVo> empPlanMasterVoList = new ArrayList<>();
        EmpPlan empPlan = empPlanMapper.selectById(empPlanTo.getPlanId());
        for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
            List<EmpPlanDetailVo> empPlanDetailVoList = new ArrayList<>();

            EmpPlanMasterVo empPlanMasterVo = new EmpPlanMasterVo();
            empPlanMasterVo.setPlanId(empPlan.getId());
            empPlanMasterVo.setPlanName(empPlan.getPlanName());
            empPlanMasterVo.setGridId(empPlan.getGridId());
            empPlanMasterVo.setEmpPlanEmployeeId(empPlanEmployee.getId());
            Grid grid = gridMap.get(empPlan.getGridId());
            if (null != grid) {
                empPlanMasterVo.setGridName(grid.getName());
            }
            empPlanMasterVo.setWorkRuleId(empPlan.getWorkRuleId());
            WorkRule workRule = workRuleMap.get(empPlan.getWorkRuleId());
            if (null != workRule) {
                empPlanMasterVo.setWorkRuleName(workRule.getRuleName());
            }
            empPlanMasterVo.setEmployeeId(empPlanEmployee.getEmployeeId());
            TlEmployee tlEmployee = tlEmployeeMap.get(empPlanEmployee.getEmployeeId());
            if (null != tlEmployee) {
                empPlanMasterVo.setEmployeeName(tlEmployee.getName());
                SysDepart sysDepart = sysDepartMap.get(tlEmployee.getEmpldeptid());
                if (null != sysDepart) {
                    empPlanMasterVo.setDepartmentId(sysDepart.getId());
                    empPlanMasterVo.setDepartmentName(sysDepart.getDepartName());
                }
            }
            List<EmpPlanDate> empPlanDateList1 = empPlanDateMap.get(empPlan.getId());
            for (EmpPlanDate empPlanDate : empPlanDateList1) {
                List<EmpPlanDateCurrentMonthVo> dateList = getDate(empPlanDate);


                for (EmpPlanDateCurrentMonthVo empPlanDateCurrentMonthVo : dateList) {
                    EmpPlanDetailVo empPlanDetailVo = new EmpPlanDetailVo();
                    empPlanDetailVo.setDate(empPlanDateCurrentMonthVo.getDate());
                    empPlanDetailVo.setEmpPlanDateId(empPlanDate.getId());

                    WorkGroup workGroup = workGroupMap.get(empPlanDate.getWorkGroupId());
                    if (null != workGroup) {
                        empPlanDetailVo.setWorkGroupId(workGroup.getWorkGroupId());
                        empPlanDetailVo.setKaoQinType(workGroup.getKaoqinType());
                        String[] kaoQinIds = workGroup.getKaoqinIds().split(",");
                        List<WorkKaoqin> workKaoqinList = new ArrayList<>();
                        for (String kaoQinId : kaoQinIds) {
                            WorkKaoqin workKaoqin = workKaoQinMap.get(kaoQinId);
                            workKaoqinList.add(workKaoqin);
                        }
                        empPlanDetailVo.setWorkKaoQinList(workKaoqinList);
                    }
                    List<EmpPlanWorkGroup> empPlanWorkGroupList1 = empPlanWorkGroupMap.get(empPlanEmployee.getEmployeeId());
                    if (null != empPlanWorkGroupList1) {
                        for (EmpPlanWorkGroup empPlanWorkGroup : empPlanWorkGroupList1) {
                            EmpPlanDetailVo empPlanDetailVo1 = new EmpPlanDetailVo();
                            empPlanDetailVo1.setDate(empPlanWorkGroup.getDate());
                            empPlanDetailVo1.setEmpPlanDateId(empPlanWorkGroup.getEmpPlanDateId());
                            empPlanDetailVo1.setEmpPlanWorkGroupId(empPlanWorkGroup.getId());
                            empPlanDetailVo1.setEmpPlanEmployeeId(empPlanWorkGroup.getEmpPlanEmployeeId());
                            WorkGroup workGroup1 = workGroupMap.get(empPlanWorkGroup.getWorkGroupId());
                            if (null != workGroup1) {
                                empPlanDetailVo1.setWorkGroupId(workGroup1.getWorkGroupId());
                                empPlanDetailVo1.setKaoQinType(workGroup1.getKaoqinType());
                                String[] kaoQinIds = workGroup1.getKaoqinIds().split(",");
                                List<WorkKaoqin> workKaoqinList = new ArrayList<>();
                                for (String kaoQinId : kaoQinIds) {
                                    WorkKaoqin workKaoqin = workKaoQinMap.get(kaoQinId);
                                    workKaoqinList.add(workKaoqin);
                                }
                                empPlanDetailVo1.setWorkKaoQinList(workKaoqinList);
                            }

                            empPlanDetailVoList.add(empPlanDetailVo1);
                        }
                    }
                    empPlanDetailVoList.add(empPlanDetailVo);
                }
            }
            empPlanMasterVo.setEmpPlanDetailVoList(empPlanDetailVoList);
            empPlanMasterVoList.add(empPlanMasterVo);
        }
        return empPlanPage(empPlanTo, empPlanMasterVoList);
    }

    public List<EmpPlanDateCurrentMonthVo> getDate(EmpPlanDate empPlanDate) throws ParseException {
        List<EmpPlanDateCurrentMonthVo> empPlanDateCurrentMonthVoList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(empPlanDate.getStartDate()));

        for (long d = cal.getTimeInMillis(); d <= sdf.parse(empPlanDate.getEndDate()).getTime(); d = getDateUpgrade(cal)) {
            EmpPlanDateCurrentMonthVo empPlanDateCurrentMonthVo = new EmpPlanDateCurrentMonthVo();
            empPlanDateCurrentMonthVo.setDate(sdf.format(d));
            empPlanDateCurrentMonthVoList.add(empPlanDateCurrentMonthVo);
        }
        return empPlanDateCurrentMonthVoList;
    }

    @Override
    public EmpPlanMessageVo create(EmpPlanTo empPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<Grid> gridListOne = gridMapper.selectList(new QueryWrapper<>());
            Map<String, Grid> gridMap = gridListOne.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

            List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());
            Map<String, TlEmployee> tlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

            List<String> gridList = empPlanTo.getGridList();
            for (String gridId : gridList) {
                //生成排班计划主表
                EmpPlan empPlan = new EmpPlan();
                empPlan.setId(StringUtils.uuid());
                if (empPlanTo.getModule() == 1) {
                    Grid grid1 = gridMap.get(gridId);
                    if (null != grid1) {
                        empPlan.setPlanName(empPlanTo.getPlanName() + "-" + grid1.getName());
                    }
                } else {
                    empPlan.setPlanName(empPlanTo.getPlanName());
                }
                empPlan.setGridId(gridId);
                empPlan.setWorkRuleId(empPlanTo.getWorkRuleId());
                empPlan.setDepartmentId(empPlanTo.getDepartmentId());
                empPlan.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                empPlanMapper.insert(empPlan);
                //生成排班计划人员从表
                List<String> employeeList = empPlanTo.getEmployeeList();
                for (String employeeId : employeeList) {
                    EmpPlanEmployee empPlanEmployee = new EmpPlanEmployee();
                    empPlanEmployee.setId(StringUtils.uuid());
                    empPlanEmployee.setEmployeeId(employeeId);
                    TlEmployee tlEmployee = tlEmployeeMap.get(employeeId);
                    if (null != tlEmployee) {
                        empPlanEmployee.setDepartmentId(tlEmployee.getEmpldeptid());
                    }
                    empPlanEmployee.setPlanId(empPlan.getId());
                    empPlanEmployee.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    empPlanEmployeeMapper.insert(empPlanEmployee);
                }
                //生成排班计划日期从表
                List<EmpPlanDateTo> empPlanDateToList = empPlanTo.getEmpPlanDateToList();
                Map<String, List<EmpPlanDateTo>> empPlanDateToMap = empPlanDateToList.stream().collect(Collectors.groupingBy(EmpPlanDateTo::getWorkGroupId));
                for (String workGroupId : empPlanDateToMap.keySet()) {

                    List<EmpPlanDateTo> empPlanDateTos = empPlanDateToMap.get(workGroupId);
                    if (null != empPlanDateTos) {
                        Boolean aBoolean = checkDate(empPlanDateTos, 0);
                        if (aBoolean) {
                            EmpPlanDate empPlanDate = new EmpPlanDate();
                            for (int i = 0; i < empPlanDateTos.size(); i++) {
                                empPlanDate.setId(StringUtils.uuid());
                                empPlanDate.setPlanId(empPlan.getId());
                                empPlanDate.setWorkGroupId(empPlanDateTos.get(i).getWorkGroupId());
                                empPlanDate.setStartDate(empPlanDateTos.get(0).getDate());
                                empPlanDate.setEndDate(empPlanDateTos.get(i).getDate());
                                empPlanDate.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                            }
                            empPlanDateMapper.insert(empPlanDate);
                        } else {
                            for (EmpPlanDateTo empPlanDateTo : empPlanDateTos) {
                                EmpPlanDate empPlanDate = new EmpPlanDate();
                                empPlanDate.setId(StringUtils.uuid());
                                empPlanDate.setPlanId(empPlan.getId());
                                empPlanDate.setWorkGroupId(empPlanDateTo.getWorkGroupId());
                                empPlanDate.setStartDate(empPlanDateTo.getDate());
                                empPlanDate.setEndDate(empPlanDateTo.getDate());
                                empPlanDate.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                empPlanDateMapper.insert(empPlanDate);
                            }
                        }
                    }
                }
            }
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        EmpPlanMessageVo empPlanMessageVo = new EmpPlanMessageVo();
        empPlanMessageVo.setMessage(message);
        return empPlanMessageVo;
    }


    @Override
    public List<EmpPlan> repeatPlanName(EmpPlanTo empPlanTo) {
        List<Grid> gridListOne = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridListOne.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<String> planNameList = new ArrayList<>();
        List<String> gridIdList = empPlanTo.getGridList();
        for (String gridId : gridIdList) {
            if (empPlanTo.getModule() == 1) {
                Grid grid1 = gridMap.get(gridId);
                if (null != grid1) {
                    planNameList.add(empPlanTo.getPlanName() + "-" + grid1.getName());
                }
            } else {
                planNameList.add(empPlanTo.getPlanName());
            }
        }
        QueryWrapper<EmpPlan> empPlanQueryWrapper = new QueryWrapper<>();
        empPlanQueryWrapper.lambda().in(EmpPlan::getPlanName, planNameList);
        List<EmpPlan> empPlanList = empPlanMapper.selectList(empPlanQueryWrapper);
        return empPlanList;
    }

    @Override
    @Transactional
    public EmpPlanMessageVo update(EmpPlanTo empPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());
            Map<String, TlEmployee> tlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

            List<String> ids = empPlanTo.getIds();
            for (String id : ids) {
                EmpPlan empPlan = empPlanMapper.selectById(id);
                empPlan.setPlanName(empPlanTo.getPlanName());
                empPlan.setGridId(empPlanTo.getGridId());
                empPlan.setDepartmentId(empPlanTo.getDepartmentId());
                empPlan.setWorkRuleId(empPlanTo.getWorkRuleId());
                empPlan.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                empPlanMapper.updateById(empPlan);

                if (empPlanTo.getFlag() == 0) {
                    QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
                    empPlanEmployeeQueryWrapper.lambda().eq(EmpPlanEmployee::getPlanId, id);
                    List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);
                    Map<String, List<EmpPlanEmployee>> empPlanEmployeeMap = empPlanEmployeeList.stream().collect(Collectors.groupingBy(EmpPlanEmployee::getEmployeeId));

                    List<String> employeeList = empPlanTo.getEmployeeList();
                    for (String employeeId : employeeList) {
                        List<EmpPlanEmployee> empPlanEmployeeList1 = empPlanEmployeeMap.get(employeeId);
                        if (null == empPlanEmployeeList1) {
                            EmpPlanEmployee empPlanEmployee = new EmpPlanEmployee();
                            empPlanEmployee.setId(StringUtils.uuid());
                            empPlanEmployee.setEmployeeId(employeeId);
                            empPlanEmployee.setPlanId(empPlan.getId());
                            empPlanEmployee.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                            empPlanEmployeeMapper.insert(empPlanEmployee);
                        } else {
                            for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList1) {
                                empPlanEmployee.setEmployeeId(empPlanEmployee.getEmployeeId());
                                empPlanEmployee.setPlanId(empPlanEmployee.getPlanId());
                                empPlanEmployee.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                empPlanEmployeeMapper.insert(empPlanEmployee);
                            }

                        }
                    }
                } else if (empPlanTo.getFlag() == 1) {
                    List<String> empPlanEmployeeIdList = new ArrayList<>();
                    QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
                    empPlanEmployeeQueryWrapper.lambda().eq(EmpPlanEmployee::getPlanId, id);
                    empPlanEmployeeQueryWrapper.lambda().in(EmpPlanEmployee::getEmployeeId, empPlanTo.getEmployeeList());
                    List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);
                    for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
                        empPlanEmployeeIdList.add(empPlanEmployee.getId());
                    }
                    if (!empPlanEmployeeIdList.isEmpty()) {
                        empPlanEmployeeMapper.deleteBatchIds(empPlanEmployeeIdList);
                    }
                } else {
                    QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
                    empPlanEmployeeQueryWrapper.lambda().eq(EmpPlanEmployee::getPlanId, id);
                    List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);
                    List<String> empPlanEmployeeIdList = new ArrayList<>();
                    for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
                        empPlanEmployeeIdList.add(empPlanEmployee.getId());
                    }
                    if (!empPlanEmployeeIdList.isEmpty()) {
                        empPlanEmployeeMapper.deleteBatchIds(empPlanEmployeeIdList);
                    }
                    QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
                    empPlanDateQueryWrapper.lambda().eq(EmpPlanDate::getPlanId, id);
                    List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
                    List<String> empPlanDateIdList = new ArrayList<>();
                    for (EmpPlanDate empPlanDate : empPlanDateList) {
                        empPlanDateIdList.add(empPlanDate.getId());
                    }
                    if (!empPlanDateIdList.isEmpty()) {
                        empPlanDateMapper.deleteBatchIds(empPlanDateIdList);
                    }
                    //生成排班计划人员从表
                    List<String> employeeList = empPlanTo.getEmployeeList();
                    for (String employeeId : employeeList) {
                        EmpPlanEmployee empPlanEmployee = new EmpPlanEmployee();
                        empPlanEmployee.setId(StringUtils.uuid());
                        empPlanEmployee.setEmployeeId(employeeId);
                        TlEmployee tlEmployee = tlEmployeeMap.get(employeeId);
                        if (null != tlEmployee) {
                            empPlanEmployee.setDepartmentId(tlEmployee.getEmpldeptid());
                        }
                        empPlanEmployee.setPlanId(empPlan.getId());
                        empPlanEmployee.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                        empPlanEmployeeMapper.insert(empPlanEmployee);
                    }
                    //生成排班计划日期从表
                    List<EmpPlanDateTo> empPlanDateToList = empPlanTo.getEmpPlanDateToList();
                    Map<String, List<EmpPlanDateTo>> empPlanDateToMap = empPlanDateToList.stream().collect(Collectors.groupingBy(EmpPlanDateTo::getWorkGroupId));
                    for (String workGroupId : empPlanDateToMap.keySet()) {
                        List<EmpPlanDateTo> empPlanDateTos = empPlanDateToMap.get(workGroupId);
                        if (null != empPlanDateTos) {
                            Boolean aBoolean = checkDate(empPlanDateTos, 0);
                            if (aBoolean) {
                                EmpPlanDate empPlanDate = new EmpPlanDate();
                                for (int i = 0; i < empPlanDateTos.size(); i++) {
                                    empPlanDate.setId(StringUtils.uuid());
                                    empPlanDate.setPlanId(empPlan.getId());
                                    empPlanDate.setWorkGroupId(empPlanDateTos.get(i).getWorkGroupId());
                                    empPlanDate.setStartDate(empPlanDateTos.get(0).getDate());
                                    empPlanDate.setEndDate(empPlanDateTos.get(i).getDate());
                                    empPlanDate.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                }
                                empPlanDateMapper.insert(empPlanDate);
                            } else {
                                for (EmpPlanDateTo empPlanDateTo : empPlanDateTos) {
                                    EmpPlanDate empPlanDate = new EmpPlanDate();
                                    empPlanDate.setId(StringUtils.uuid());
                                    empPlanDate.setPlanId(empPlan.getId());
                                    empPlanDate.setWorkGroupId(empPlanDateTo.getWorkGroupId());
                                    empPlanDate.setStartDate(empPlanDateTo.getDate());
                                    empPlanDate.setEndDate(empPlanDateTo.getDate());
                                    empPlanDate.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                    empPlanDateMapper.insert(empPlanDate);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        EmpPlanMessageVo empPlanMessageVo = new EmpPlanMessageVo();
        empPlanMessageVo.setMessage(message);
        return empPlanMessageVo;
    }

    @Override
    public EmpPlanMessageVo updateByDay(EmpPlanTo empPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
//        List<String> empPlanDateIdList = empPlanTo.getEmpPlanDateIdList();
//        if (null != empPlanDateIdList && !empPlanDateIdList.isEmpty()) {
//            QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
//            empPlanDateQueryWrapper.lambda().in(EmpPlanDate::getId, empPlanTo.getEmpPlanDateIdList());
//            List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
//            for (EmpPlanDate empPlanDate : empPlanDateList) {
//                empPlanDate.setWorkGroupId(empPlanTo.getWorkGroupId());
//                empPlanDateMapper.updateById(empPlanDate);
//            }
//        } else {
            if (StringUtils.isNotEmpty(empPlanTo.getEmpPlanWorkGroupId())) {
                EmpPlanWorkGroup empPlanWorkGroup = empPlanWorkGroupMapper.selectById(empPlanTo.getEmpPlanWorkGroupId());
                empPlanWorkGroup.setWorkGroupId(empPlanTo.getWorkGroupId());
                empPlanWorkGroupMapper.updateById(empPlanWorkGroup);
            } else {
                //生成排班计划日期从表
                EmpPlanWorkGroup empPlanWorkGroup = new EmpPlanWorkGroup();
                empPlanWorkGroup.setId(StringUtils.uuid());
                empPlanWorkGroup.setEmployeeId(empPlanTo.getEmployeeId());
                empPlanWorkGroup.setWorkGroupId(empPlanTo.getWorkGroupId());
                empPlanWorkGroup.setDate(empPlanTo.getDate());
                empPlanWorkGroup.setEmpPlanEmployeeId(empPlanTo.getEmpPlanEmployeeId());
                empPlanWorkGroup.setEmpPlanDateId(empPlanTo.getEmpPlanDateId());
                empPlanWorkGroup.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                empPlanWorkGroupMapper.insert(empPlanWorkGroup);
            }
//        }
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        EmpPlanMessageVo empPlanMessageVo = new EmpPlanMessageVo();
        empPlanMessageVo.setMessage(message);
        return empPlanMessageVo;
    }

    @Override
    public EmpPlanMessageVo deleteMaster(EmpPlanTo empPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
            List<String> ids = empPlanTo.getIds();
            empPlanMapper.deleteBatchIds(ids);

            List<String> empPlanEmployeeIdList = new ArrayList<>();

            QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
            empPlanEmployeeQueryWrapper.lambda().in(EmpPlanEmployee::getPlanId, ids);
            List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);

            for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
                empPlanEmployeeIdList.add(empPlanEmployee.getId());
            }
            empPlanEmployeeMapper.deleteBatchIds(empPlanEmployeeIdList);

            List<String> empPlanDateIdList = new ArrayList<>();
            QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
            empPlanDateQueryWrapper.lambda().in(EmpPlanDate::getPlanId, ids);
            List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
            for (EmpPlanDate empPlanDate : empPlanDateList) {
                empPlanDateIdList.add(empPlanDate.getId());
            }
            empPlanDateMapper.deleteBatchIds(empPlanDateIdList);
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        EmpPlanMessageVo empPlanMessageVo = new EmpPlanMessageVo();
        empPlanMessageVo.setMessage(message);
        return empPlanMessageVo;
    }

    @Override
    public EmpPlanMessageVo deleteDetail(EmpPlanTo empPlanTo) throws ParseException {
        String message = "";
        if (DateUtils.checkDateTime()) {
//        List<String> empPlanWorkGroupIdList = empPlanTo.getEmpPlanWorkGroupIdList();
//        if (null != empPlanWorkGroupIdList && !empPlanWorkGroupIdList.isEmpty()) {
//            List<String> empPlanEmployeeIdList = new ArrayList<>();
//            List<String> empPlanDateIdList = new ArrayList<>();
//            QueryWrapper<EmpPlanWorkGroup> empPlanWorkGroupQueryWrapper = new QueryWrapper<>();
//            empPlanWorkGroupQueryWrapper.lambda().in(EmpPlanWorkGroup::getId, empPlanWorkGroupIdList);
//            List<EmpPlanWorkGroup> empPlanWorkGroupList = empPlanWorkGroupMapper.selectList(empPlanWorkGroupQueryWrapper);
//            for (EmpPlanWorkGroup empPlanWorkGroup : empPlanWorkGroupList) {
//                empPlanEmployeeIdList.add(empPlanWorkGroup.getEmpPlanEmployeeId());
//                empPlanDateIdList.add(empPlanWorkGroup.getEmpPlanDateId());
//            }
//            empPlanEmployeeMapper.deleteBatchIds(empPlanEmployeeIdList);
//
//            empPlanDateMapper.deleteBatchIds(empPlanDateIdList);
//
//            empPlanWorkGroupMapper.deleteBatchIds(empPlanWorkGroupIdList);
//        } else {
            empPlanEmployeeMapper.deleteBatchIds(empPlanTo.getEmpPlanEmployeeIdList());
        } else {
            message = "23点-1点系统数据处理中....,请过后继续尝试";
        }
        EmpPlanMessageVo empPlanMessageVo = new EmpPlanMessageVo();
        empPlanMessageVo.setMessage(message);
        return empPlanMessageVo;
//            empPlanDateMapper.deleteBatchIds(empPlanTo.getEmpPlanDateIdList());
//        }

    }

    @Override
    public WorkGroupByCarPlanVo deleteWorkGroup(EmpPlanTo empPlanTo) {
        WorkGroupByCarPlanVo workGroupByCarPlanVo = new WorkGroupByCarPlanVo();
        List<String> empPlanIds = new ArrayList<>();
        QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
        empPlanDateQueryWrapper.lambda().in(EmpPlanDate::getWorkGroupId, empPlanTo.getWorkGroupIdList());
        List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
        if (!empPlanDateList.isEmpty()) {
            for (EmpPlanDate empPlanDate : empPlanDateList) {
                empPlanIds.add(empPlanDate.getId());
            }

        } else {
            for (String workGroupId : empPlanTo.getWorkGroupIdList()) {
                workGroupMapper.deleteById(workGroupId);
            }
        }
        workGroupByCarPlanVo.setWorkPlanIdList(empPlanIds);
        return workGroupByCarPlanVo;
    }

    @Override
    public WorkGroupByCarPlanVo deleteWorkRule(EmpPlanTo empPlanTo) {
        WorkGroupByCarPlanVo workGroupByCarPlanVo = new WorkGroupByCarPlanVo();
        List<String> empPlanIds = new ArrayList<>();
        QueryWrapper<EmpPlan> empPlanQueryWrapper = new QueryWrapper<>();
        empPlanQueryWrapper.lambda().in(EmpPlan::getWorkRuleId, empPlanTo.getWorkRuleIdList());
        List<EmpPlan> empPlanList = empPlanMapper.selectList(empPlanQueryWrapper);
        if (!empPlanList.isEmpty()) {
            for (EmpPlan empPlan : empPlanList) {
                empPlanIds.add(empPlan.getId());
            }

        } else {
            for (String workRuleId : empPlanTo.getWorkRuleIdList()) {
                workRuleMapper.deleteById(workRuleId);
            }
        }
        workGroupByCarPlanVo.setWorkPlanIdList(empPlanIds);
        return workGroupByCarPlanVo;
    }

    public EmpPlanReadOneVo read(EmpPlanTo empPlanTo) throws ParseException {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkRule> workRuleList = workRuleMapper.selectList(new QueryWrapper<>());
        Map<String, WorkRule> workRuleMap = workRuleList.stream().collect(Collectors.groupingBy(WorkRule::getWorkRuleId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = workGroupList.stream().collect(Collectors.groupingBy(WorkGroup::getWorkGroupId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        List<WorkKaoqin> workKaoQinList = workKaoqinMapper.selectList(new QueryWrapper<>());
        Map<String, WorkKaoqin> workKaoQinMap = workKaoQinList.stream().collect(Collectors.groupingBy(WorkKaoqin::getWorkKaoqinId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        Map<String, SysDepart> sysDepartMap = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId,
                Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));


        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());
        Map<String, TlEmployee> tlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), values -> values.get(0))));

        EmpPlan empPlan = empPlanMapper.selectById(empPlanTo.getPlanId());
        EmpPlanMasterVo empPlanMasterVo = new EmpPlanMasterVo();
        empPlanMasterVo.setPlanName(empPlan.getPlanName());
        empPlanMasterVo.setPlanId(empPlan.getId());
        Grid grid = gridMap.get(empPlan.getGridId());
        if (null != grid) {
            empPlanMasterVo.setGridName(grid.getName());
            empPlanMasterVo.setGridId(grid.getGridId());
        }
        WorkRule workRule = workRuleMap.get(empPlan.getWorkRuleId());
        if (null != workRule) {
            empPlanMasterVo.setWorkRuleId(workRule.getWorkRuleId());
            empPlanMasterVo.setWorkRuleName(workRule.getRuleName());
        }

        QueryWrapper<EmpPlanDate> empPlanDateQueryWrapper = new QueryWrapper<>();
        empPlanDateQueryWrapper.lambda().eq(EmpPlanDate::getPlanId, empPlanTo.getPlanId());
        List<EmpPlanDate> empPlanDateList = empPlanDateMapper.selectList(empPlanDateQueryWrapper);
        List<EmpPlanDetailVo> empPlanDetailVoList = new ArrayList<>();
        for (EmpPlanDate empPlanDate : empPlanDateList) {
            List<EmpPlanDateCurrentMonthVo> dateList = getDate(empPlanDate);
            for (EmpPlanDateCurrentMonthVo empPlanDateCurrentMonthVo : dateList) {
                EmpPlanDetailVo empPlanDetailVo = new EmpPlanDetailVo();
                empPlanDetailVo.setDate(empPlanDateCurrentMonthVo.getDate());
                empPlanDetailVo.setEmpPlanDateId(empPlanDate.getId());
                WorkGroup workGroup = workGroupMap.get(empPlanDate.getWorkGroupId());
                if (null != workGroup) {
                    empPlanDetailVo.setWorkGroupId(workGroup.getWorkGroupId());
                    empPlanDetailVo.setKaoQinType(workGroup.getKaoqinType());
                    empPlanDetailVo.setWorkGroupName(workGroup.getName());
                    String[] kaoQinIds = workGroup.getKaoqinIds().split(",");
                    List<WorkKaoqin> workKaoqinList = new ArrayList<>();
                    for (String kaoQinId : kaoQinIds) {
                        WorkKaoqin workKaoqin = workKaoQinMap.get(kaoQinId);
                        workKaoqinList.add(workKaoqin);
                    }
                    empPlanDetailVo.setWorkKaoQinList(workKaoqinList);
                }
                empPlanDetailVoList.add(empPlanDetailVo);
            }
        }
//        empPlanMasterVo.setEmpPlanDetailVoList(empPlanDetailVoList);

        QueryWrapper<EmpPlanEmployee> empPlanEmployeeQueryWrapper = new QueryWrapper<>();
        empPlanEmployeeQueryWrapper.lambda().eq(EmpPlanEmployee::getPlanId, empPlanTo.getPlanId());
        List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper.selectList(empPlanEmployeeQueryWrapper);
        List<String> employeeIdList = new ArrayList<>();
        List<String> employeeNameList = new ArrayList<>();
        for (EmpPlanEmployee empPlanEmployee : empPlanEmployeeList) {
            employeeIdList.add(empPlanEmployee.getEmployeeId());
            TlEmployee tlEmployee = tlEmployeeMap.get(empPlanEmployee.getEmployeeId());
            if (null != tlEmployee) {
                employeeNameList.add(tlEmployee.getName());
                SysDepart sysDepart = sysDepartMap.get(tlEmployee.getEmpldeptid());
                if (null != sysDepart) {
                    empPlanMasterVo.setDepartmentId(sysDepart.getId());
                    empPlanMasterVo.setDepartmentName(sysDepart.getDepartName());
                }
            }
        }
        EmpPlanReadOneVo empPlanReadOneVo = new EmpPlanReadOneVo();
        empPlanReadOneVo.setEmpPlanMasterVo(empPlanMasterVo);
        empPlanReadOneVo.setEmpPlanDetailVoList(empPlanDetailVoList);
        empPlanReadOneVo.setEmployeeIdList(employeeIdList);
        empPlanReadOneVo.setEmployeeNameList(employeeNameList);

        return empPlanReadOneVo;
    }

    private EmpPlanPageVo empPlanPage(EmpPlanTo empPlanTo, List list) {
        // 分页
        Long page = StringUtils.isEmpty(empPlanTo.getPage()) ? 1 : empPlanTo.getPage();
        Long size = StringUtils.isEmpty(empPlanTo.getSize()) ? 10 : empPlanTo.getSize();
        EmpPlanPageVo empPlanPageVo = new EmpPlanPageVo();

        empPlanPageVo.setTotal((long) list.size());
        empPlanPageVo.setCurrent(page);
        empPlanPageVo.setSize(size);
        BigDecimal pageSizeB = new BigDecimal(size);
        BigDecimal sizeB = new BigDecimal(list.size());
        empPlanPageVo.setPages(Long.valueOf(sizeB.divide(pageSizeB, BigDecimal.ROUND_CEILING).toString()));

        empPlanPageVo.setList(new ArrayList<>());
        for (int i = 0; i < list.size(); i++) {
            if (i >= size * (page - 1) && i < size * page) {
                empPlanPageVo.getList().add(list.get(i));
            }
        }
        return empPlanPageVo;
    }

    @Override
    public List<EmpPlanSummaryTableVO> selectSummaryTable(EmpPlanSummaryTableTO empPlanSummaryTableTO) {
        String depId = empPlanSummaryTableTO.getDepId();
        String gridId = empPlanSummaryTableTO.getGridId();
        String planId = empPlanSummaryTableTO.getPlanId();
        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<RptEmployeeWork>();
        rptEmployeeWorkQueryWrapper.lambda().between(RptEmployeeWork::getWorkDate
                , Tools.getYearMonthDayString(empPlanSummaryTableTO.getStartTime()), Tools.getYearMonthDayString(empPlanSummaryTableTO.getEndTime()));
        if (StringUtils.isNotEmpty(empPlanSummaryTableTO.getPlanName())) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getPlanName, empPlanSummaryTableTO.getPlanName());
        }
        if (StringUtils.isNotEmpty(depId)) {
            List<String> depIds = departService.getChildIdsByIId(depId);
            List<TlEmployee> employeeList = tlEmployeeMapper
                    .selectList(new QueryWrapper<TlEmployee>().lambda().in(TlEmployee::getDeptid, depIds));
            if (CollectionUtils.isNotEmpty(employeeList)) {
                rptEmployeeWorkQueryWrapper.lambda().in(RptEmployeeWork::getPlanName, employeeList.stream().map(TlEmployee::getId).collect(Collectors.toList()));
            }
        }
        if (StringUtils.isNotEmpty(planId)) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getPlanId, planId);
        }
        if (StringUtils.isNotEmpty(gridId)) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getGridId, gridId);
        }
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        List<EmpPlanSummaryTableVO> empPlanSummaryTableVOList = new ArrayList<>();
        QueryWrapper<AlarmPerson> alarmPersonQueryWrapper = new QueryWrapper<>();
        alarmPersonQueryWrapper.lambda().between(AlarmPerson::getAlarmTime, empPlanSummaryTableTO.getStartTime(), empPlanSummaryTableTO.getEndTime());
        List<AlarmPerson> alarmPersonList = alarmPersonMapper.selectList(alarmPersonQueryWrapper);
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().between(DlbjViolation::getDate, empPlanSummaryTableTO.getStartTime(), empPlanSummaryTableTO.getEndTime());
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        Map<String, List<AlarmPerson>> alarmPersonMap = GroupingByUtils.getGroupingForMapList(alarmPersonList, AlarmPerson::getGridId);
        Map<String, List<DlbjViolation>> dlbjViolationMap = GroupingByUtils.getGroupingForMapList(dlbjViolationList, DlbjViolation::getGridId);
        Map<String, TlEmployee> employeeMap = tlEmployeeService.getEmpMap(new QueryWrapper<>());
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());
        Map<String, List<RptEmployeeWork>> rptMap = GroupingByUtils.getGroupingForMapList(rptEmployeeWorkList, RptEmployeeWork::getPlanId);
        QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<Grid>();
        Map<String, Grid> gridMap = gridService.getGridMap(gridQueryWrapper);
        List<String> planIds = rptEmployeeWorkList.stream().map(RptEmployeeWork::getPlanId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        for (String id : planIds) {
            empPlanSummaryTableVOList.add(EmpPlanSummaryTableVO.buildEmpPlanSummaryTableVO(employeeMap,
                    departMap, CollectionUtils.isNotEmpty(rptMap.get(id)) ? rptMap.get(id) : new ArrayList<>()
                    , alarmPersonMap, dlbjViolationMap, gridMap));
        }
        empPlanSummaryTableVOList = empPlanSummaryTableVOList.stream().distinct().collect(Collectors.toList());
        return empPlanSummaryTableVOList;
    }

    @Override
    public List<EmpPlanSummaryDetailTableVO> selectSummaryDetailTable(EmpPlanSummaryTableTO empPlanSummaryTableTO) {
        String depId = empPlanSummaryTableTO.getDepId();
        String gridId = empPlanSummaryTableTO.getGridId();
        String planId = empPlanSummaryTableTO.getPlanId();
        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<RptEmployeeWork>();
        rptEmployeeWorkQueryWrapper.lambda().between(RptEmployeeWork::getWorkDate
                , Tools.getYearMonthDayString(empPlanSummaryTableTO.getStartTime()), Tools.getYearMonthDayString(empPlanSummaryTableTO.getEndTime()));
        if (StringUtils.isNotEmpty(empPlanSummaryTableTO.getPlanName())) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getPlanName, empPlanSummaryTableTO.getPlanName());
        }
        if (StringUtils.isNotEmpty(empPlanSummaryTableTO.getEmpId())) {
            rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getEmployeeId, empPlanSummaryTableTO.getEmpId());
        }
        if (StringUtils.isNotEmpty(depId)) {
            List<String> depIds = departService.getChildIdsByIId(depId);
            List<TlEmployee> employeeList = tlEmployeeMapper
                    .selectList(new QueryWrapper<TlEmployee>().lambda().in(TlEmployee::getDeptid, depIds));
            if (CollectionUtils.isNotEmpty(employeeList)) {
                rptEmployeeWorkQueryWrapper.lambda().in(RptEmployeeWork::getPlanName, employeeList.stream().map(TlEmployee::getId).collect(Collectors.toList()));
            }
        }
        if (StringUtils.isNotEmpty(planId)) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getPlanId, planId);
        }
        if (StringUtils.isNotEmpty(gridId)) {
            rptEmployeeWorkQueryWrapper.lambda().like(RptEmployeeWork::getGridId, gridId);
        }
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        List<EmpPlanSummaryDetailTableVO> empPlanSummaryTableVOList = new ArrayList<>();
        QueryWrapper<AlarmPerson> alarmPersonQueryWrapper = new QueryWrapper<>();
        alarmPersonQueryWrapper.lambda().between(AlarmPerson::getAlarmTime, empPlanSummaryTableTO.getStartTime(), empPlanSummaryTableTO.getEndTime());
        List<AlarmPerson> alarmPersonList = alarmPersonMapper.selectList(alarmPersonQueryWrapper);
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().between(DlbjViolation::getDate, empPlanSummaryTableTO.getStartTime(), empPlanSummaryTableTO.getEndTime());
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        Map<String, List<AlarmPerson>> alarmPersonMap = GroupingByUtils.getGroupingForMapList(alarmPersonList, AlarmPerson::getGridId);
        Map<String, List<DlbjViolation>> dlbjViolationMap = GroupingByUtils.getGroupingForMapList(dlbjViolationList, DlbjViolation::getGridId);
        Map<String, TlEmployee> employeeMap = tlEmployeeService.getEmpMap(new QueryWrapper<>());
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());

        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            empPlanSummaryTableVOList.add(EmpPlanSummaryDetailTableVO.buildEmpPlanSummaryDetailTableVO(rptEmployeeWork, employeeMap,
                    departMap, alarmPersonMap, dlbjViolationMap));
        }
        empPlanSummaryTableVOList = empPlanSummaryTableVOList.stream()
                .filter(empPlanSummaryDetailTableVO -> StringUtils.isNotEmpty(empPlanSummaryDetailTableVO.getWorkTimeLength())).distinct().collect(Collectors.toList());
        return empPlanSummaryTableVOList;
    }

    @Override
    public List<EmpPlanForExcelVO> selectEmpPlanForExcel(List<EmpPlan> empPlanList) {
        List<EmpPlanForExcelVO> empPlanForExcelVOList = new ArrayList<>();
        for (EmpPlan empPlan : empPlanList) {
            empPlanForExcelVOList.add(EmpPlanForExcelVO.buildEmpPlanForExcelVO(
                    empPlan, gridService.getGridMap(new QueryWrapper<>()), tlEmployeeService.getEmpMap(new QueryWrapper<>()),
                    operationService.getTlOperationMap(new QueryWrapper<>()), workGroupService.getWorkGroupMap(new QueryWrapper<>())));
        }
        return empPlanForExcelVOList;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Boolean excelReader(ExcelReaderTO excelReaderTO) throws ExcelReaderDataException {
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<Grid>().lambda().isNotNull(Grid::getName));
        Map<String, Grid> gridMap = GroupingByUtils.getGroupingForMapEntity(gridList, Grid::getName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        List<TlEmployee> employeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());
        Map<String, TlEmployee> employeeMap = GroupingByUtils.getGroupingForMapEntity(employeeList, TlEmployee::getName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        ExcelUtil<EmpPlanForExcelVO> util = new ExcelUtil<>(EmpPlanForExcelVO.class);
        MultipartFile file = excelReaderTO.getFiles();
        if (Arrays.stream(ExcelTypeEnum.values()).noneMatch(s -> s.getName().equals(FileTypeUtils.getFileType(file.getOriginalFilename().toLowerCase())))) {
            throw new ExcelReaderDataException("导入文件格式错误");
        }
        InputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<EmpPlanForExcelVO> empPlanForExcelVOList = new ArrayList<>();
        try {
            empPlanForExcelVOList = util.importExcel(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (empPlanForExcelVOList.isEmpty()) {
            throw new ExcelReaderDataException("导入内容不能为空");
        }
        List<EmpPlan> empPlanList2 = empPlanMapper.selectList(new QueryWrapper<>());
        Map<String, EmpPlan> empPlanMap = GroupingByUtils.getGroupingForMapEntity(empPlanList2, EmpPlan::getPlanName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        List<WorkGroup> workGroupList = workGroupMapper.selectList(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = GroupingByUtils.getGroupingForMapEntity(workGroupList, WorkGroup::getName, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        String str = "";
        int num = 1;
        List<EmpPlan> empPlanList = new ArrayList<>();
        List<EmpPlanEmployee> empPlanEmployeeList = new ArrayList<>();
        List<EmpPlanDate> empPlanDateList = new ArrayList<>();
        EmpPlan empPlan = new EmpPlan();
        EmpPlanEmployee empPlanEmployee = new EmpPlanEmployee();
        EmpPlanDate empPlanDate = new EmpPlanDate();
        for (EmpPlanForExcelVO empPlanForExcelVO : empPlanForExcelVOList) {
            //EmpPlan
            String planId = UUID.randomUUID().toString();
            empPlan = new EmpPlan();
            empPlan.setId(planId);
            empPlan.setCreateTime(Tools.getYearMonthDayString(new Date()));
            if (StringUtils.isNotEmpty(empPlanForExcelVO.getPlanName())) {
                if (empPlanMap.containsKey(empPlanForExcelVO.getPlanName())) {
                    str += "第" + num + "条的“‘排班名称’:‘" + empPlanForExcelVO.getPlanName() + "’”" + "已存在;##";
                } else {
                    empPlan.setPlanName(empPlanForExcelVO.getPlanName());
                }
            } else {
                str += "第" + num + "条的‘排班名称’不能为空;##";
            }
            if (StringUtils.isNotEmpty(empPlanForExcelVO.getGridName())) {
                if (gridMap.containsKey(empPlanForExcelVO.getGridName())) {
                    empPlan.setGridId(gridMap.get(empPlanForExcelVO.getGridName()).getGridId());
                } else {
                    str += "第" + num + "条的“‘网格名称’:‘" + empPlanForExcelVO.getGridName() + "’”" + "不存在;##";
                }
            } else {
                str += "第" + num + "条的‘网格名称’不能为空;##";
            }
            empPlanList.add(empPlan);
            //EmpPlanEmployee
            if (StringUtils.isNotEmpty(empPlanForExcelVO.getEmpName())) {
                for (String name : empPlanForExcelVO.getEmpName().split("、")) {
                    empPlanEmployee = new EmpPlanEmployee();
                    empPlanEmployee.setId(UUID.randomUUID().toString());
                    empPlanEmployee.setPlanId(planId);
                    if (employeeMap.containsKey(name)) {
                        empPlanEmployee.setEmployeeId(employeeMap.get(name).getId());
                    } else {
                        str += "第" + num + "条的“‘作业人员’:‘" + empPlanForExcelVO.getEmpName() + "’”" + "不存在;##";
                    }
                    empPlanEmployee.setCreateTime(Tools.getYearMonthDayString(new Date()));
                    empPlanEmployeeList.add(empPlanEmployee);
                }
            } else {
                str += "第" + num + "条的‘作业人员’不能为空;##";
            }
            //EmpPlanDate
            empPlanDate = new EmpPlanDate();
            empPlanDate.setId(UUID.randomUUID().toString());
            empPlanDate.setPlanId(planId);
            String workDays = empPlanForExcelVO.getWorkDays();
            if (StringUtils.isNotEmpty(workDays)) {
                if (workDays.indexOf("至") != -1) {
                    List<String> workDaysList = Arrays.asList(workDays.split("至"));
                    workDaysList = workDaysList.stream().sorted().collect(Collectors.toList());
                    empPlanDate.setStartDate(workDaysList.get(0));
                    empPlanDate.setEndDate(workDaysList.get(1));
                } else {
                    empPlanDate.setStartDate(workDays);
                    empPlanDate.setEndDate(workDays);
                }
            } else {
                str += "第" + num + "条的‘排班日期’不能为空;##";
            }
            empPlanDate.setCreateTime(Tools.getYearMonthDayString(new Date()));
            if (StringUtils.isNotEmpty(empPlanForExcelVO.getWorkGroupName())) {
                if (workGroupMap.containsKey(empPlanForExcelVO.getWorkGroupName())) {
                    empPlanDate.setWorkGroupId(workGroupMap.get(empPlanForExcelVO.getWorkGroupName()).getWorkGroupId());
                } else {
                    str += "第" + num + "条的“‘班组名称’:‘" + empPlanForExcelVO.getGridName() + "’”" + "不存在;##";
                }
            } else {
                str += "第" + num + "条的‘班组名称’不能为空;##";
            }
            empPlanDateList.add(empPlanDate);
            num++;
        }
        if (StringUtils.isNotEmpty(str)) {
            throw new ExcelReaderDataException("您导入的数据:##" + str);
        }
        Boolean i1 = saveBatch(empPlanList);
        Boolean i2 = empPlanDateService.saveBatch(empPlanDateList);
        Boolean i3 = empPlanEmployeeService.saveBatch(empPlanEmployeeList);
        return i1 && i2 && i3;
    }

    @Override
    public List<EmpPlan> selectList(SelectEmpPlanTableTO selectEmpPlanTableTO) {
        List<String> ids = selectEmpPlanTableTO.getIds();
        String planName = selectEmpPlanTableTO.getPlanName();
        Date startTime = selectEmpPlanTableTO.getStartTime();
        Date endTime = selectEmpPlanTableTO.getEndTime();
        QueryWrapper<EmpPlan> planQueryWrapper = new QueryWrapper<EmpPlan>();
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime)) {
            List<EmpPlanDate> empPlanDateList = empPlanDateMapper
                    .selectList(new QueryWrapper<EmpPlanDate>().lambda().gt(EmpPlanDate::getStartDate, startTime).le(EmpPlanDate::getEndDate, endTime));
            if (CollectionUtils.isNotEmpty(empPlanDateList)) {
                planQueryWrapper.lambda().in(EmpPlan::getId, empPlanDateList.stream().map(EmpPlanDate::getPlanId).collect(Collectors.toList()));
            }
        }
        if (StringUtils.isNotEmpty(planName)) {
            planQueryWrapper.lambda().like(EmpPlan::getPlanName, planName);
        }
        if (CollectionUtils.isNotEmpty(ids)) {
            planQueryWrapper.lambda().in(EmpPlan::getId, ids);
        }
        List<EmpPlan> resultList = empPlanMapper.selectList(planQueryWrapper);

        if (CollectionUtils.isNotEmpty(resultList)) {
            List<String> planIds = resultList.stream().map(EmpPlan::getId).collect(Collectors.toList());
            List<EmpPlanDate> empPlanDateList = empPlanDateMapper
                    .selectList(new QueryWrapper<EmpPlanDate>().lambda().in(EmpPlanDate::getPlanId, planIds));
            List<EmpPlanEmployee> empPlanEmployeeList = empPlanEmployeeMapper
                    .selectList(new QueryWrapper<EmpPlanEmployee>().lambda().in(EmpPlanEmployee::getPlanId, resultList.stream().map(EmpPlan::getId).collect(Collectors.toList())));
            Map<String, List<EmpPlanDate>> empPlanDateMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(empPlanDateList)) {
                empPlanDateMap = GroupingByUtils.getGroupingForMapList(empPlanDateList, EmpPlanDate::getPlanId);
            }
            Map<String, List<EmpPlanEmployee>> empPlanEmployeeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(empPlanEmployeeList)) {
                empPlanEmployeeMap = GroupingByUtils.getGroupingForMapList(empPlanEmployeeList, EmpPlanEmployee::getPlanId);
            }

            for (EmpPlan empPlan : resultList) {
                if (empPlanDateMap.containsKey(empPlan.getId())) {
                    empPlan.setEmpPlanDateList(empPlanDateMap.get(empPlan.getId()));
                }
                if (empPlanEmployeeMap.containsKey(empPlan.getId())) {
                    empPlan.setEmpPlanEmployeeList(empPlanEmployeeMap.get(empPlan.getId()));
                }
            }
        }
        return resultList;
    }

    /**
     * 通过计划查考勤
     *
     * @param selectEmpPlanTableTO
     * @param rptEmployeeWorkList
     * @return
     */
    @Override
    public List<ActualOperationListVo> selectWorkKaoqinList(SelectEmpPlanTableTO selectEmpPlanTableTO, List<RptEmployeeWork> rptEmployeeWorkList) {
        List<ActualOperationListVo> actualOperationListVoList = new ArrayList<>();
        String overFlag = dictTypeService.getSysDictTypeByName("job_completion_standard").getDictValue();
        if (CollectionUtils.isEmpty(rptEmployeeWorkList)) {
            return new ArrayList<>();
        }
        List<WorkGroup> workGroupList = workGroupService.list(new QueryWrapper<WorkGroup>()
                .lambda().in(WorkGroup::getWorkGroupId, rptEmployeeWorkList.stream().distinct().map(RptEmployeeWork::getGroupId).collect(toList()))
        );
        List<String> idList = new ArrayList<>();
        workGroupList.forEach(workGroup -> {
            if (StringUtils.isNotEmpty(workGroup.getKaoqinIds())) {
                List<String> ids = Arrays.asList(workGroup.getKaoqinIds().split(","));
                idList.addAll(ids);
            }
        });
        List<WorkKaoqin> workKaoqinList = workKaoqinService.list(new QueryWrapper<WorkKaoqin>()
                .lambda().in(WorkKaoqin::getWorkKaoqinId, idList));
        Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<>());
        Map<String, User> userMap = userService.getUserMap();
        QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<Grid>();
        gridQueryWrapper.lambda().eq(Grid::getGridType, 0);
        Map<String, Grid> gridMap = gridService.getGridMap(gridQueryWrapper);
        Map<String, TlEmployee> employeeMap = employeeService.getEmpMap(new QueryWrapper<>());
        Map<String, WorkGroup> workGroupMap = GroupingByUtils.getGroupingForMapEntity(workGroupList, WorkGroup::getWorkGroupId
                , Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        Map<String, List<RptEmployeeWork>> rptMap = rptEmployeeWorkList.stream().collect(Collectors.groupingBy(o->o.getPlanId()+"##"+o.getShiftId()));;
        Map<String, WorkKaoqin> workKaoqinMap = GroupingByUtils.getGroupingForMapEntity(workKaoqinList, WorkKaoqin::getWorkKaoqinId
                , Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        for (String planIdAndShiftId : rptMap.keySet()) {
            String planId =planIdAndShiftId.split("##")[0];
            String shiftId =planIdAndShiftId.split("##")[1];
            RptEmployeeWork firstRpt = rptMap.get(planIdAndShiftId).get(0);
            List<RptEmployeeWork> rptList = rptMap.get(planIdAndShiftId);
            Integer planAllCompletion = firstRpt.getAllPointsNum();;
            Integer planOverCompletion = 0;
            WorkGroup workGroup = workGroupMap.get(firstRpt.getGroupId());
            WorkKaoqin workKaoqin = workKaoqinMap.get(shiftId);
            SysDepart depart = new SysDepart();
            if (Objects.nonNull(departMap.get(firstRpt.getDepId()))) {
                depart = departMap.get(firstRpt.getDepId());
            }
            User user = new User();
            if (Objects.nonNull(userMap.get(depart.getPrincipalUid()))) {
                user = userMap.get(depart.getPrincipalUid());
            }
            BigDecimal degreeCompletion = null;
            /**
             * 开始时间
             */
            String startTime = workKaoqin.getSbsj();
            /**r
             * 结束时间
             */
            String endTime = workKaoqin.getXbsj();
            if (Objects.nonNull(workGroup) && Objects.nonNull(workGroup.getKaoqinType()) && workGroup.getKaoqinType().equals(2)) {
                if (ActualOperationListVo.getTimeBoolean()) {
                    startTime = workKaoqin.getXsbsj();
                    endTime = workKaoqin.getXxbsj();
                } else {
                    startTime = workKaoqin.getDsbsj();
                    endTime = workKaoqin.getDxbsj();
                }
            }
            List<EmpActual> empActualList = new ArrayList<EmpActual>();
            Map<String, List<RptEmployeeWork>> empRptListMap = GroupingByUtils.getGroupingForMapList(rptList, RptEmployeeWork::getEmployeeId);
            BigDecimal completion = BigDecimal.ZERO;
            List<String> pointIdList = new ArrayList<>();
            for (String employeeId : empRptListMap.keySet()) {
                Integer empAllCompletion = firstRpt.getAllPointsNum();
                Integer empOverCompletion = 0;
                for (RptEmployeeWork rpt : empRptListMap.get(employeeId)) {
                    if (StringUtils.isNotEmpty(rpt.getOverPointsId())) {
                        pointIdList.addAll(Arrays.asList(rpt.getOverPointsId().split(",")));
                    }
                    empOverCompletion += rpt.getOverPointsNum();
                }
                if (empAllCompletion > 0) {
                    completion = BigDecimal.valueOf(empOverCompletion).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(empAllCompletion), 0, BigDecimal.ROUND_HALF_UP);
                } else {
                    completion = BigDecimal.ZERO;
                }
                if(completion.compareTo(new BigDecimal(overFlag))>=0){
                    completion = new BigDecimal(100);
                }
                empActualList.add(new EmpActual(employeeId, employeeMap.get(employeeId).getName(), firstRpt.getGridName(), completion.toString()));
            }
            planOverCompletion = Integer.valueOf(String.valueOf(pointIdList.stream().distinct().count()));
            if (planAllCompletion > 0) {
                degreeCompletion = BigDecimal.valueOf(planOverCompletion).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(planAllCompletion), 0, BigDecimal.ROUND_HALF_UP);
            } else {
                degreeCompletion = BigDecimal.ZERO;
            }
            if(degreeCompletion.compareTo(new BigDecimal(overFlag))>=0){
                degreeCompletion = new BigDecimal(100);
            }
            actualOperationListVoList.add(new ActualOperationListVo(
                    firstRpt.getPlanName(), workKaoqin.getName(),
                    startTime, endTime, degreeCompletion.toString(), firstRpt.getGridName(), depart.getDepartName(), user.getName(), user.getPhone(), gridMap.get(firstRpt.getGridId()), CollectionUtils.isNotEmpty(empActualList) ?
                    empActualList.stream().sorted(Comparator.comparing(EmpActual::getDegreeCompletionDecimal).reversed()).collect(toList()) : empActualList
            ));
        }
        return actualOperationListVoList;
    }

    public List<EmployeeConfigVo> employeeConfig() {

        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        rptEmployeeWorkQueryWrapper.lambda().groupBy(RptEmployeeWork::getEmployeeId);
        rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        Map<String, List<RptEmployeeWork>> rptEmployeeWorkMap = rptEmployeeWorkList.stream().collect(Collectors.groupingBy(RptEmployeeWork::getEmployeeId));

        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(new QueryWrapper<>());

        Map<String, List<TlEmployee>> tlEmployeeGroupByWorkTypeId = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getWorktype));

        List<TlWorkType> tlWorkTypeList = tlWorkTypeMapper.selectList(new QueryWrapper<>());

        List<EmployeeConfigVo> employeeConfigVoList = new ArrayList<>();

        for (TlWorkType tlWorkType : tlWorkTypeList) {
            List<TlEmployee> tlEmployeeList1 = tlEmployeeGroupByWorkTypeId.get(tlWorkType.getId());
            if (null != tlEmployeeList1) {
                EmployeeConfigVo employeeConfigVo = new EmployeeConfigVo();
                Integer workTypeNumber = 0;
                for (TlEmployee tlEmployee : tlEmployeeList1) {
                    List<RptEmployeeWork> rptEmployeeWorkList1 = rptEmployeeWorkMap.get(tlEmployee.getId());
                    if (null != rptEmployeeWorkList1) {
                        employeeConfigVo.setWorkTypeId(tlWorkType.getId());
                        employeeConfigVo.setWorkTypeName(tlWorkType.getWorktype());
                        workTypeNumber++;
                        employeeConfigVo.setWorkTypeNumber(workTypeNumber);
                        employeeConfigVo.setWorkTypeNumberTotal(rptEmployeeWorkList.size());
                        BigDecimal bigDecimal = new BigDecimal(employeeConfigVo.getWorkTypeNumber()).divide(new BigDecimal(employeeConfigVo.getWorkTypeNumberTotal()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
                        employeeConfigVo.setPercentage(bigDecimal);
                    }
                }
                employeeConfigVoList.add(employeeConfigVo);
            }

        }
        return employeeConfigVoList;
    }

    @Override
    public Map<String, EmpPlan> getEmpMap(QueryWrapper<EmpPlan> queryWrapper) {
        List<EmpPlan> empPlanList = empPlanMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(empPlanList)) {
            return new HashMap<>();
        }
        return GroupingByUtils.getGroupingForMapEntity(empPlanList, EmpPlan::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
    }

    @Override
    public List<TreeEntity> getEmployeeShiftStatus(EmpPlanTo empPlanTo) {
        Set<String> empIds = new HashSet<>();
        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(empPlanTo.getStartDate()) && StringUtils.isNotEmpty(empPlanTo.getEndDate())) {
            rptEmployeeWorkQueryWrapper.lambda().le(RptEmployeeWork::getPlanStartTime, empPlanTo.getStartDate());
            rptEmployeeWorkQueryWrapper.lambda().ge(RptEmployeeWork::getPlanEndTime, empPlanTo.getStartDate());
        }
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            empIds.add(rptEmployeeWork.getEmployeeId());
        }
        List<TreeEntity> trees = new ArrayList<>();
        if (!empIds.isEmpty()) {
            QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
            tlEmployeeQueryWrapper.lambda().in(TlEmployee::getId, empIds);
            List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(tlEmployeeQueryWrapper);
            Map<String, TlEmployee> tlEmployeeMap = tlEmployeeList.stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), tlEmployees -> tlEmployees.get(0))));
            Map<String, TlEmployee> tlEmployeeCollect = tlEmployeeService.list().stream().collect(Collectors.groupingBy(TlEmployee::getId, Collectors.collectingAndThen(Collectors.toList(), tlEmployees -> tlEmployees.get(0))));
            List<TreeEntity> treeList = sysDepartMapper.getTreeList();
            List<TreeEntity> emplTree = tlEmployeeMapper.getEmplTree();
            for (TreeEntity treeEntity : emplTree) {
                TlEmployee tlEmployee = tlEmployeeMap.get(treeEntity.getId());
                TlEmployee tlEmployee1 = tlEmployeeCollect.get(treeEntity.getId());
                if (null != tlEmployee1) {
                    treeEntity.setEmployeeTypeId(tlEmployee1.getWorktype());
                }
                if (null != tlEmployee) {
                    treeEntity.setShiftStatus(1);
                } else {
                    treeEntity.setShiftStatus(0);
                }
            }
            treeList.addAll(emplTree);

            trees = TreeUtil.RecursiveAddress(treeList);
        }


        return trees;
    }


    @Override
    @DataScope(deptAlias = "department_id", userAlias = "employee_id")
    public PageVo attendanceCount(BaseEntity entity, AttendanceCountTo attendanceCountTo) {
        //当前日期
        String day = DateUtil.formatDate(new Date());

        List<AttendanceCountVo> attendanceCountVoList = new ArrayList<>();

        //电子围栏的数据
        QueryWrapper<Grid> gridQueryWrapper = new QueryWrapper<>();
        gridQueryWrapper.lambda().eq(Grid::getGridType, Constants.GRID_TYPE_ELECTRICFENCE);
        Map<String, Grid> gridMap = gridService.getGridMap(gridQueryWrapper);

        //根据条件查询打卡记录
        QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
        employeePunchCardQueryWrapper.lambda().between(EmployeePunchCard::getPunchCardDate, attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
        if (StringUtils.isNotEmpty(attendanceCountTo.getEmployeeId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getEmployeeId, attendanceCountTo.getEmployeeId());
        }
        if (StringUtils.isNotEmpty(attendanceCountTo.getGridId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getGridId, attendanceCountTo.getGridId());
        }
        if (StringUtils.isNotEmpty(attendanceCountTo.getDepartmentId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getDepartmentId, attendanceCountTo.getDepartmentId());
        }
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            employeePunchCardQueryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        List<EmployeePunchCard> employeePunchCardList = employeePunchCardMapper.selectList(employeePunchCardQueryWrapper);

        //根据人分组生成map
        Map<String, List<EmployeePunchCard>> listMapByEmployeeId = employeePunchCardList.stream().collect(Collectors.groupingBy(EmployeePunchCard::getEmployeeId, toList()));
        listMapByEmployeeId.keySet().forEach(t->{
            //每个人的打卡记录
            List<EmployeePunchCard> employeePunchCards = listMapByEmployeeId.get(t);
            if (Objects.nonNull(employeePunchCards) && employeePunchCards.size() > 0){

                AttendanceCountVo attendanceCountVo = new AttendanceCountVo();
                attendanceCountVo.setEmployeeId(employeePunchCards.get(0).getEmployeeId());
                attendanceCountVo.setEmployeeName(employeePunchCards.get(0).getEmployeeName());
                attendanceCountVo.setDeptName(employeePunchCards.get(0).getDepartmentName());
                Grid grid = gridMap.get(employeePunchCards.get(0).getGridId());
                if (Objects.nonNull(grid)){
                    attendanceCountVo.setGridName(grid.getName());
                }
                //根据该人的打卡日期分组生成map
                Map<String, List<EmployeePunchCard>> listMapByPunchCardDate = employeePunchCards.stream().collect(Collectors.groupingBy(EmployeePunchCard::getPunchCardDate, toList()));
                attendanceCountVo.setPlanDays(listMapByPunchCardDate.size());
                attendanceCountVo.setPlannumbers(employeePunchCards.size());
                //正常打卡的记录 (实际上班打卡时间早于迟到规定的时间 且 实际下班打卡时间晚于早退规定的时间)
                int normalityNum = employeePunchCards.stream().filter(a -> Objects.nonNull(a.getActualWorkTime()) && Objects.nonNull(a.getActualAfterWorkTime())
                        && DateUtil.parseDateTime(day + " " + a.getActualWorkTime()).compareTo(DateUtils.getpostponetimesoradvancetimesByMinute(DateUtil.parseDateTime(day + " " + a.getWorkTime()), -a.getLaterInProvision())) < 0
                        && DateUtil.parseDateTime(day + " " + a.getActualAfterWorkTime()).compareTo(DateUtils.getpostponetimesoradvancetimesByMinute(DateUtil.parseDateTime(day + " " + a.getAfterWorkTime()), a.getLeaveEarlyProvision())) > 0).collect(toList()).size();
                attendanceCountVo.setNormality(normalityNum);
                //迟到打卡的记录 (实际上班打卡时间晚于迟到规定的时间)
                int laterInNum = employeePunchCards.stream().filter(a -> Objects.nonNull(a.getActualWorkTime()) && DateUtil.parseDateTime(day + " " + a.getActualWorkTime()).compareTo(DateUtils.getpostponetimesoradvancetimesByMinute(DateUtil.parseDateTime(day + " " + a.getWorkTime()), -a.getLaterInProvision())) >= 0).collect(toList()).size();
                attendanceCountVo.setLaterIn(laterInNum);
                //早退打卡的记录
                int leaveEarlyNum = employeePunchCards.stream().filter(a -> Objects.nonNull(a.getActualAfterWorkTime()) && DateUtil.parseDateTime(day + " " + a.getActualAfterWorkTime()).compareTo(DateUtils.getpostponetimesoradvancetimesByMinute(DateUtil.parseDateTime(day + " " + a.getAfterWorkTime()), a.getLeaveEarlyProvision())) <= 0).collect(toList()).size();
                attendanceCountVo.setLeaveEarly(leaveEarlyNum);
                //旷工的记录
                int noWorkNum = employeePunchCards.stream().filter(a -> Constants.STATE_ABNORMAL == a.getNoWork()).collect(toList()).size();
                attendanceCountVo.setNoWork(noWorkNum);
                //缺卡的记录
                int lackCardNum = employeePunchCards.stream().filter(a -> Constants.STATE_ABNORMAL == a.getLackCard()).collect(toList()).size();
                attendanceCountVo.setLackCard(lackCardNum);

                //获取每天的打卡记录状态
                List<AttendanceCountDayVo> attendanceCountDayVoList = new ArrayList<>();
                //获取开始时间和结束时间内的所有日期
                List<String> dateList = Tools.getBetweenDate(attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
                dateList.forEach(k->{
                    AttendanceCountDayVo attendanceCountDayVo = new AttendanceCountDayVo();
                    List<EmployeePunchCard> employeePunchCardList1 = listMapByPunchCardDate.get(k);
                    if (Objects.nonNull(employeePunchCardList1) && employeePunchCardList1.size() > 0){
                        employeePunchCardList1.forEach(j->{
                            if (Constants.STATE_ABNORMAL == j.getLaterIn() || Constants.STATE_ABNORMAL == j.getLeaveEarly() || Constants.STATE_ABNORMAL == j.getNoWork() || Constants.STATE_ABNORMAL == j.getLackCard()){
                                attendanceCountDayVo.setStatus(Constants.STATE_ABNORMAL);
                            }
                        });
                    } else {
                        attendanceCountDayVo.setStatus(Constants.STATE_NOSCHEDULE);
                    }
                    attendanceCountDayVo.setDate(k);
                    attendanceCountDayVoList.add(attendanceCountDayVo);
                });
                attendanceCountVo.setAttendanceCountDayVo(attendanceCountDayVoList);
                attendanceCountVoList.add(attendanceCountVo);
            }
        });


//        QueryWrapper<GridBind> gridBindQueryWrapper = new QueryWrapper<>();
//        gridBindQueryWrapper.lambda().eq(GridBind::getType, "emp");
//        gridBindQueryWrapper.lambda().between(GridBind::getBindTime, attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
//        List<GridBind> gridBindList = gridBindMapper.selectList(gridBindQueryWrapper);
//        Map<String, List<GridBind>> gridBindMap = gridBindList.stream().collect(Collectors.groupingBy(GridBind::getItemId));
//
//        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
//        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));
//
//
//        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
//        rptEmployeeWorkQueryWrapper.lambda().between(RptEmployeeWork::getWorkDate, attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
//        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
//        Map<String, List<RptEmployeeWork>> rptEmployeeWorkMap = rptEmployeeWorkList.stream().collect(Collectors.groupingBy(RptEmployeeWork::getWorkDate));
//
//        Integer normality = 0; //正常
//        Integer laterIn = 0;//迟到
//        Integer leaveEarly = 0;//早退
//        Integer noWork = 0;//旷工
//        Integer lackCard = 0;
//        Set<String> stringSet = new HashSet<>();
//        Map<String, List<EmployeePunchCard>> employeePunchCardGroupByDate = employeePunchCardList.stream().collect(Collectors.groupingBy(EmployeePunchCard::getEmployeeId));
//        for (String employeeId : employeePunchCardGroupByDate.keySet()) {
//            AttendanceCountVo attendanceCountVo = new AttendanceCountVo();
//            Set<String> gridNameList = new HashSet<>();
//            List<EmployeePunchCard> employeePunchCardList1 = employeePunchCardGroupByDate.get(employeeId);
//            if (null != employeePunchCardList1) {
//                for (EmployeePunchCard employeePunchCard : employeePunchCardList1) {
//                    attendanceCountVo.setEmployeeId(employeePunchCard.getEmployeeId());
//                    attendanceCountVo.setEmployeeName(employeePunchCard.getEmployeeName());
//                    attendanceCountVo.setDeptName(employeePunchCard.getDepartmentName());
//                    attendanceCountVo.setPlanDays(Integer.parseInt(betweenDayNumber.toString()));
//                    List<GridBind> gridBindList1 = gridBindMap.get(employeePunchCard.getEmployeeId());
//                    if (null != gridBindList1) {
//                        for (GridBind gridBind : gridBindList1) {
//                            stringSet.add(gridBind.getGridId());
//                            Grid grid = gridMap.get(gridBind.getGridId());
//                            if (null != grid) {
//                                gridNameList.add(grid.getName());
//                            }
//                        }
//                        attendanceCountVo.setPlanNumbers(stringSet.size() * 2 * attendanceCountVo.getPlanDays());
//                    } else {
//                        attendanceCountVo.setPlanNumbers(attendanceCountVo.getPlanDays() * 2);
//                    }
//                    if (null == employeePunchCard.getWorkTime() || null == employeePunchCard.getAfterWorkTime()) {
//                        lackCard++;
//                    }
//                    attendanceCountVo.setLackCard(lackCard);
//                    if (null != employeePunchCard.getLaterIn() && employeePunchCard.getLaterIn() == 1) {
//                        laterIn++;
//                    }
//                    attendanceCountVo.setLaterIn(laterIn);
//                    if (null != employeePunchCard.getLeaveEarly() && employeePunchCard.getLeaveEarly() == 1) {
//                        leaveEarly++;
//                    }
//                    attendanceCountVo.setLeaveEarly(leaveEarly);
//                    if (null != employeePunchCard.getNoWork() && employeePunchCard.getNoWork() == 1) {
//                        noWork++;
//                    }
//                    attendanceCountVo.setNoWork(noWork);
//                    if (null != employeePunchCard.getLeaveEarly() && null != employeePunchCard.getLaterIn() && null != employeePunchCard.getNoWork()) {
//                        if (employeePunchCard.getLaterIn() == 0 && employeePunchCard.getLeaveEarly() == 0 && employeePunchCard.getNoWork() == 0) {
//                            normality++;
//                        }
//                        attendanceCountVo.setNormality(normality);
//                    }
//
//                    if (null != rptEmployeeWorkMap.get(employeePunchCard.getPunchCardDate())) {
//                        AttendanceCountDayVo attendanceCountDayVo = new AttendanceCountDayVo();
//                        attendanceCountDayVo.setDate(employeePunchCard.getPunchCardDate());
//                        attendanceCountDayVo.setStatus(0);
//                        if (null != employeePunchCard.getLaterIn() && null != employeePunchCard.getLeaveEarly() && null != employeePunchCard.getNoWork() && null != employeePunchCard.getLackCard()) {
//                            if (employeePunchCard.getLaterIn() == 1 || employeePunchCard.getLeaveEarly() == 1 || employeePunchCard.getNoWork() == 1 || employeePunchCard.getLackCard() == 1) {
//                                attendanceCountDayVo.setStatus(1);
//                            }
//                        }
//
//                        attendanceCountDayVoList.add(attendanceCountDayVo);
//                        attendanceCountVo.setAttendanceCountDayVo(attendanceCountDayVoList);
//                    }
//                }
//                attendanceCountVo.setGridName(String.join(",", gridNameList));
//            }
//            attendanceCountVoList.add(attendanceCountVo);
//        }

        return Tools.page(attendanceCountTo.getPage(), attendanceCountTo.getSize(), attendanceCountVoList);
    }

    @Override
    public PageVo attendanceDetail(AttendanceCountTo attendanceCountTo) {

        List<AttendanceDetailVo> attendanceDetailVoList = new ArrayList<>();
        List<String> gridIds = new ArrayList<>();

        //打卡记录
        QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
        employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getEmployeeId, attendanceCountTo.getEmployeeId());
        employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getManCarType, Constants.TYPE_PERSON);
        if (StringUtils.isNotEmpty(attendanceCountTo.getGridId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getGridId, attendanceCountTo.getGridId());
        }
        if (StringUtils.isNotEmpty(attendanceCountTo.getDepartmentId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getDepartmentId, attendanceCountTo.getDepartmentId());
        }
        employeePunchCardQueryWrapper.lambda().between(EmployeePunchCard::getPunchCardDate, attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
        List<EmployeePunchCard> employeePunchCards = employeePunchCardMapper.selectList(employeePunchCardQueryWrapper);
        employeePunchCards.forEach(t->{
            gridIds.add(t.getGridId());
        });

        //网格快照数据
        QueryWrapper<GridSnapshot> gridSnapshotQueryWrapper = new QueryWrapper<>();
        gridSnapshotQueryWrapper.lambda().in(GridSnapshot::getGridId, gridIds);
        List<GridSnapshot> gridList = gridSnapshotMapper.selectList(gridSnapshotQueryWrapper);
        Map<String, List<GridSnapshot>> gridMap = gridList.stream().collect(Collectors.groupingBy(GridSnapshot::getGridId));

        //打卡记录按照日期分组
        Map<String, List<EmployeePunchCard>> employeePunchCardGroupByDateMap = employeePunchCards.stream().collect(Collectors.groupingBy(EmployeePunchCard::getPunchCardDate));
        for (String date : employeePunchCardGroupByDateMap.keySet()) {
            List<EmployeePunchCard> employeePunchCardList1 = employeePunchCardGroupByDateMap.get(date);
            AttendanceDetailVo attendanceDetailVo = new AttendanceDetailVo();

            List<AttendanceDetailDayVo> attendanceDetailDayVoList = new ArrayList<>();
            if (null != employeePunchCardList1) {
                Map<String, List<EmployeePunchCard>> employeePunchCardGroupByEmpIdMap = employeePunchCardList1.stream().collect(Collectors.groupingBy(EmployeePunchCard::getEmployeeId));
                for (String employeeId : employeePunchCardGroupByEmpIdMap.keySet()) {

                    List<EmployeePunchCard> employeePunchCardList2 = employeePunchCardGroupByEmpIdMap.get(employeeId);
                    if (null != employeePunchCardList2) {
                        for (EmployeePunchCard employeePunchCard : employeePunchCardList2) {
                            attendanceDetailVo.setEmployeeName(employeePunchCard.getEmployeeName());
                            attendanceDetailVo.setDeptName(employeePunchCard.getDepartmentName());
                            attendanceDetailVo.setPunchCardDate(employeePunchCard.getPunchCardDate());
                            List<GridSnapshot> grids = gridMap.get(employeePunchCard.getGridId());

                            AttendanceDetailDayVo attendanceDetailDayVo = new AttendanceDetailDayVo();
                            if (Objects.nonNull(grids) && grids.size() > 0) {
                                List<GridSnapshot> gridSnapshotList = grids.stream().filter(a -> employeePunchCard.getPunchCardDate().equals(a.getDate())).collect(toList());
                                if (Objects.nonNull(gridSnapshotList) && gridSnapshotList.size() > 0 && StringUtils.isNotEmpty(gridSnapshotList.get(0).getGridId()) && StringUtils.isNotEmpty(gridSnapshotList.get(0).getName())){
                                    attendanceDetailDayVo.setGridId(gridSnapshotList.get(0).getGridId());
                                    attendanceDetailDayVo.setGridName(gridSnapshotList.get(0).getName());
                                }
                            }
                            attendanceDetailDayVo.setId(employeePunchCard.getId());
                            attendanceDetailDayVo.setWorkTime(employeePunchCard.getActualWorkTime());
                            attendanceDetailDayVo.setAfterWorkTime(employeePunchCard.getActualAfterWorkTime());
                            attendanceDetailDayVoList.add(attendanceDetailDayVo);
                        }
                    }
                    attendanceDetailVo.setAttendanceDetailDayVoList(attendanceDetailDayVoList);
                }
            }
            attendanceDetailVoList.add(attendanceDetailVo);

            //按照日期倒序
            Collections.sort(attendanceDetailVoList,(o1, o2) -> {
                String d1 = o1.getPunchCardDate();
                String d2 = o2.getPunchCardDate();
                return d2.compareTo(d1);
            });
        }
        return Tools.page(attendanceCountTo.getPage(), attendanceCountTo.getSize(), attendanceDetailVoList);
    }

    @Override
    public List attendanceCountChars(AttendanceCountTo attendanceCountTo) {
        QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
        employeePunchCardQueryWrapper.lambda().between(EmployeePunchCard::getPunchCardDate, attendanceCountTo.getStartDate(), attendanceCountTo.getEndDate());
        if (StringUtils.isNotEmpty(attendanceCountTo.getDepartmentId())) {
            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getDepartmentId, attendanceCountTo.getDepartmentId());
        }
        List<EmployeePunchCard> employeePunchCardList = employeePunchCardMapper.selectList(employeePunchCardQueryWrapper);
        List list = new ArrayList();

        if (attendanceCountTo.getType() == 0) {
            Map<String, List<EmployeePunchCard>> employeePunchCardMap = employeePunchCardList.stream().collect(Collectors.groupingBy(EmployeePunchCard::getPunchCardDate));
            for (String date : employeePunchCardMap.keySet()) {
                List list1 = new ArrayList();
                list1.add(date);
                Integer laterIn = 0;  // 0 正常  1早退
                Integer leaveEarly = 0;
                Integer noWork = 0;
                Integer lackCard = 0;
                Integer normal = 0;
                List<EmployeePunchCard> employeePunchCardList1 = employeePunchCardMap.get(date);
                if (null != employeePunchCardList1) {

                    for (EmployeePunchCard employeePunchCard : employeePunchCardList1) {

                        if (null != employeePunchCard.getLaterIn() && employeePunchCard.getLaterIn() == 1) {
                            laterIn++;
                        }
                        if (null != employeePunchCard.getLeaveEarly() && employeePunchCard.getLeaveEarly() == 1) {
                            leaveEarly++;
                        }
                        if (null != employeePunchCard.getNoWork() && employeePunchCard.getNoWork() == 1) {
                            noWork++;
                        }
                        if (null != employeePunchCard.getLackCard() && employeePunchCard.getLackCard() == 1) {
                            lackCard++;
                        }
                        if (null != employeePunchCard.getLaterIn() && null != employeePunchCard.getLeaveEarly() && null != employeePunchCard.getNoWork() && null != employeePunchCard.getLackCard()) {
                            if (employeePunchCard.getLaterIn() == 0 || employeePunchCard.getLeaveEarly() == 0 || employeePunchCard.getNoWork() == 0 || employeePunchCard.getLackCard() == 0) {
                                normal++;
                            }
                        }
                    }
                }
                list1.add(normal);
                list1.add(laterIn);
                list1.add(leaveEarly);
                list1.add(noWork);
                list1.add(lackCard);

                list.add(list1);
            }
        } else {
            Map<String, List<EmployeePunchCard>> employeePunchCardMap = employeePunchCardList.stream().collect(Collectors.groupingBy(EmployeePunchCard::getPunchCardMonth));
            for (String date : employeePunchCardMap.keySet()) {
                List list1 = new ArrayList();
                list1.add(date);
                Integer laterIn = 0;  // 0 正常  1早退
                Integer leaveEarly = 0;
                Integer noWork = 0;
                Integer lackCard = 0;
                Integer normal = 0;
                List<EmployeePunchCard> employeePunchCardList1 = employeePunchCardMap.get(date);
                if (null != employeePunchCardList1) {

                    for (EmployeePunchCard employeePunchCard : employeePunchCardList1) {

                        if (employeePunchCard.getLaterIn() == 1) {
                            laterIn++;
                        }
                        if (employeePunchCard.getLeaveEarly() == 1) {
                            leaveEarly++;
                        }
                        if (employeePunchCard.getNoWork() == 1) {
                            noWork++;
                        }
                        if (employeePunchCard.getLackCard() == 1) {
                            lackCard++;
                        }
                        if (employeePunchCard.getLaterIn() == 0 || employeePunchCard.getLeaveEarly() == 0 || employeePunchCard.getNoWork() == 0 || employeePunchCard.getLackCard() == 0) {
                            normal++;
                        }
                    }
                }
                list1.add(normal);
                list1.add(laterIn);
                list1.add(leaveEarly);
                list1.add(noWork);
                list1.add(lackCard);

                list.add(list1);
            }
        }


        return list;
    }

}
